<template>
  <div class="address">
      <van-nav-bar
  title="新增地址"
  left-text="返回"
  right-text="我的地址"
  left-arrow
  @click-left="onClickLeft"
  @click-right="onClickRight"
/>
    <van-address-edit 
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data"; // 是级联数据
import { addaddress } from "../../api/address";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
    };
  },
  computed: {},
  watch: {},

  methods: {
        
    async onSave(e) {
      console.log(e);
      let add = e.province + "-" + e.city + "-" + e.county + e.addressDetail;
      console.log(add);
      this.$store.commit("address/savereceiver",e.name)
      this.$store.commit("address/savemobile",e.tel)
      this.$store.commit("address/saveregions",add)
        const res = await addaddress({
          receiver:this.$store.state.address.receiver,
          mobile:this.$store.state.address.mobile,
          regions:this.$store.state.address.regions,
        });
        if(res.code == "success"){
          Toast.success(res.message);
          this.$router.push("/addresslist")
        }else{
          Toast.fail(res.message)
        }

    },
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    onClickLeft(){
      this.$router.go(-1);
    },
    onClickRight(){
      this.$router.push("/addresslist")
    }
  },
  created() {
  },
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
